package com.szpx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szpx.entity.Film;
import com.szpx.entity.Movie;
import com.szpx.entity.User;
import com.szpx.service.CinemaHomeService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CinemaHomeController {

    @Autowired
    private CinemaHomeService cinemaHomeService;

    /**
     *  根据id查询影片的信息
     */

    @GetMapping("/selectMovieById/{id}")
    public String selectMovieById(@PathVariable("id") Integer id, Model model) {
        Movie movie = cinemaHomeService.selectMovieById(id);
        if (movie != null){
            model.addAttribute("Movie",movie);
            return "cinemahome/detail_movie";
        }else {
            return null;
        }
    }



    /**
     * 添加影片
     */

    @RequestMapping("/addMovie")
    public String addMovie(Movie movie){
        int num = cinemaHomeService.addMovie(movie);
        if (num > 0)
            return "redirect:/clist";
        else
            return null;
    }
    /**
     * 跳转到添加影片界面
     */
    @RequestMapping("/toaddMovie")
    public String toaddMovie(HttpSession session){
        return "cinemahome/clist";
    }

    /**
     * 修改影片信息
     */
    @RequestMapping("/updateMovie")
    public String updateMovie(Movie movie){
        int num = cinemaHomeService.updateMovie(movie);
        if (num > 0)
            return "redirect:/clist";
        else
            return null;
    }
    /**
     * 去影片修改页面
     */
    @GetMapping("/toUpdateMovie/{id}")
    public String toUpdateMoviePage(@PathVariable("id")Integer id
            ,Model model){
        Movie movie = cinemaHomeService.selectMovieById(id);
        if (movie != null){
            model.addAttribute("movie",movie); //将查询出来的信息 传到修改页面
            return "cinemahome/update_movie";
        }else {
            return null;
        }
    }


    // 删除电影
    @RequestMapping("/deleMovie/{id}")
    public String deleMovie(@PathVariable("id") Integer id) {
        int num = cinemaHomeService.deleMovie(id);
        if (num > 0)
            return "redirect:/clist"; // 删除成功后重定向到电影列表
        else
            return null; // 删除失败提示
        }

    /**
     * 多条件分页查询所有电影
     */
    @GetMapping("/clist")
    public String getList(
            @RequestParam(defaultValue = "1") int pno,
            @RequestParam(defaultValue = "3") int pageSize,
            Movie movie, @NotNull Model model) {
        PageHelper.startPage(pno, pageSize); //开始分页
        List<Movie> clist = cinemaHomeService.getListByMovie(movie);
        PageInfo<Movie> page = new PageInfo<Movie>(clist);
        model.addAttribute("clist", clist); //将查询出来的数据放入model对象中
        model.addAttribute("page", page);
        model.addAttribute("title", movie.getTitle());
        model.addAttribute("genre", movie.getGenre());
        model.addAttribute("pno", pno);
        return "cinemahome/clist";
    }
}

